<?php
namespace app\command;

use think\console\Command;
use think\console\Input;
use think\console\input\Option;
use think\console\Output;
use think\Db;

/**
 * Class Profit
 * @package app\command
 */
class CardDistribution extends Command
{
    protected $contractHelper = null;
    protected  $mtHelper =null;
    protected function configure()
    {
        $this->setName('CardDistribution')
            ->addOption('id', 'i', Option::VALUE_REQUIRED, '商户id')
            ->addOption('start', 's', Option::VALUE_REQUIRED, '开始卡号')
            ->addOption('end', 'e', Option::VALUE_REQUIRED, '结束卡号')
            ->setDescription('卡号分派');
    }


    protected function execute(Input $input, Output $output){
        $options = $input->getOptions();
        $id = $options['id'];
        $start = intval($options['start']);
        $end = intval($options['end']);
        $obj = Db::table("tb_card_shop")->where("type",1)->where('id', $id)->find();

        if (!$obj){
           echo "代理商不存在 \n";
           return;
        }

        if ($start<1000000000){
            return ['status' => 'n', 'info' => '开始卡号错误'];
        }
        if ($start>9999999999){
            return ['status' => 'n', 'info' => '开始卡号错误'];
        }
        if ($end<1000000000){
            return ['status' => 'n', 'info' => '结束卡号错误'];
        }
        if ($end>9999999999){
            return ['status' => 'n', 'info' => '结束卡号错误'];
        }

        $sqldata=[];
        $sqldata["agent_time"]=time();
        $sqldata["agent_id"]=$id;
        $res = Db::table("tb_card_info")->where("status",0)->where("agent_id is null")
            ->where("card",">=",$start)->where("card","<=",$end)->update($sqldata);
        if ($res) {
            echo "操作成功 \n";
        } else {
            echo "操作失败 \n";
        }
    }


}
